package parser.nodes;

import codegen.CodeGen;
import scanner.Token;

public class ModuleNode extends Node {

	private Token moduleName;
	private DeclarationsNode decls;
	private StatementSequenceNode stmts;

	public ModuleNode(Token moduleName, DeclarationsNode decls, StatementSequenceNode stmts) {
		this.moduleName = moduleName;
		this.decls = decls;
		this.stmts = stmts;
	}

	@Override
	public void print() {
		p("Module:");
		indent();
		p("MODULE " + moduleName + ";");
		decls.print();
		p("BEGIN");
		stmts.print();
		p("END " + moduleName + ".");
		unindent();
	}

	@Override
	public void abstractPrint() {
		p("MODULE");
		indent();
		p(moduleName);
		decls.abstractPrint();
		stmts.abstractPrint();
		unindent();
	}

	@Override
	public void compile(CodeGen c) throws CompileError {
		int codeStart = c.getLabel();
		c.outstr("JMP, " + codeStart);
		decls.compile(c);
		c.outstr("//Modulanfang");
		c.outstr("LABEL, " + codeStart);
		c.outstr("PUSHI, 0");
		c.outstr("SETFP");
		c.outstr("PUSHI, " + c.nextFreeAddress()); //Stackpointer zeigt immer auf das nächste FREIE Feld
		c.outstr("SETSP");
		//SL1-Register setzen
		c.outstr("PUSHI, 0");
		c.outstr("PUSHI, 1");
		c.outstr("SETSL");
		stmts.compile(c);
		c.outstr("STOP");
	}
}
